<template>
    <el-container>
        <el-main class="wrapper">
            <section class="panel cms-content" v-loading="loading"
                     element-loading-text="数据提交中..."
                     element-loading-spinner="el-icon-loading" style="min-height: 550px">
                <div class="panel-body">
                    <el-row>
                        <el-col :span="12" style="padding-left: 15px">
                            <el-form :model="form" label-position="top" ref="form">
                                <el-form-item label="首次注册赠送积分数：">
                                    <el-input style="width: 350px" v-model="form.value.register"></el-input>
                                </el-form-item>
                                <el-form-item label="点赞赠送积分数：">
                                    <el-input style="width: 350px" v-model="form.value.praise"></el-input>
                                </el-form-item>
                                <el-form-item label="点赞赠送积分次数/每天：">
                                    <el-input style="width: 350px" v-model="form.value.praise_day"></el-input>
                                </el-form-item>
                                <el-form-item label="发布文章赠送积分数/每篇：">
                                    <el-input style="width: 350px" v-model="form.value.publish"></el-input>
                                </el-form-item>
                                <div class="form-btn" style="padding-left: 155px">
                                    <el-button type="primary" @click="update('form')">提交保存</el-button>
                                </div>
                            </el-form>
                        </el-col>
                        <el-col :span="4"></el-col>
                    </el-row>
                </div>
            </section>
        </el-main>
    </el-container>
</template>
<script>
    import {mapGetters} from 'vuex';
    import {ConfigInfo, ConfigUpdate} from '@/utils/request';
    export default {
        data() {
            return {
                form: {
                    id: 2,
                    key:'integral',
                    value: {
                        register:0,praise:0,praise_day:0,publish:0,
                    },
                },
            }
        },
        activated() {
            this.form = {
                id: 2,
                key:'integral',
                value: {
                    register:0,praise:0,praise_day:0,publish:0,
                },
            };
            this.integral();
        },
        computed: {
            ...mapGetters(['loading']),
        },
        methods: {
            integral() {
                this.$store.dispatch('setLoading', true);
                ConfigInfo({params:{key:'integral'}}).then((res) => {
                    if(res.code){
                        this.$message.error(res.msg);
                    }else{
                        if(res.data.value) this.form = res.data;
                    }
                    this.$store.dispatch('setLoading', false);
                });
            },
            //更新
            update() {
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        this.$store.dispatch('setLoading', true);
                        ConfigUpdate(this.form).then((response) => {
                            if (response.code) {
                                this.$message.error(response.msg);
                            } else {
                                this.$message({
                                    type: 'success',
                                    duration: '1000',
                                    message: response.msg,
                                    onClose: () => {
                                        this.integral();
                                    }
                                });
                            }
                        });
                    }
                });

            },
        },
    }
</script>